# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import os
import json


# 在线视频-短视频管道
class ShortVideoAreaPipeline(object):
    def __init__(self):
        self.file_path = os.path.join(".", "在线视频", "短视频区.json")
        self.file = open(self.file_path, "w", encoding="utf-8")

    def process_item(self, item, spider):
        data = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.file.write(data)

    def close_spider(self, spider):
        self.file.close()


# 在线视频-美女视频
class BeautyLiveItemPipeline(object):
    def __init__(self):
        self.file_path = os.path.join(".", "在线视频", "美女主播.json")
        self.file = open(self.file_path, "w", encoding="utf-8")

    def process_item(self, item, spider):
        data = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.file.write(data)

    def close_spider(self, spider):
        self.file.close()


# 在线视频-国产精品
class DomesticProductsPipeline(object):
    def __init__(self):
        self.file_path = os.path.join(".", "在线视频", "国产精品.json")
        self.file = open(self.file_path, "w", encoding="utf-8")

    def process_item(self, item, spider):
        data = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.file.write(data)

    def close_spider(self, spider):
        self.file.close()


# 情色小说-都市激情
class UrbanPassionPipeline(object):
    def __init__(self):
        self.file_path = os.path.join(".", "情色小说", "另类小说.txt")
        self.file = open(self.file_path, "w", encoding="utf-8")

    def process_item(self, item, spider):
        content_list = item["content"]
        # 写入标题
        self.file.write(item["title"])
        self.file.write("\n")
        self.file.write("\n")
        for i in content_list:
            self.file.write(i)
            self.file.write("\n")
        self.file.write("\n\n")

    def close_spider(self, spider):
        self.file.close()


# 激情图区-自拍偷拍
class CandidSelfiePipeline(object):
    def __init__(self):
        self.file_path = os.path.join(".", "激情图区", "自拍偷拍.json")
        self.file = open(self.file_path, "w", encoding="utf-8")
        self.file.write("[")

    def process_item(self, item, spider):
        print(item)
        data = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.file.write(data)

    def close_spider(self, spider):
        self.file.write("]")
        self.file.close()
